﻿using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductAvailableScreenSnapshootService
    {
        /// <summary>
        /// 创建可用屏幕快照
        /// </summary>
        /// <param name="insuranceOrderId">保单表InsuranceOrder主键ID</param>
        /// <param name="serviceProjectType">保险信息类型：和ServiceProductType是同一个枚举，根据ServiceProjectType去不同的表取数据，比如手机维修保险信息，就去CellPhoneInsuranceInfo表取</param>
        /// <returns></returns>
        public bool CreateSnapshoot(long insuranceOrderId, ServiceProductType serviceProjectType)
        {
            var result = true;
            var cellPhoneInsuranceInfoList = ServiceFactory.CellPhoneInsuranceInfoServiceInstance.LoadByInsuranceID(insuranceOrderId);//todo 根据ServiceProjectType去不同的表取数据
            if (cellPhoneInsuranceInfoList.Any())
            {
                var cellPhoneServiceProjectIDs =
                    cellPhoneInsuranceInfoList.Select(o => o.CellPhoneServiceProjectID.Value).ToList();
                var productsAvailableScreenTypeList =
                    ServiceFactory.InsuranceProductsAvailableScreenTypeServiceInstance.GetInsuranceProductsAvailableScreenTypeList(cellPhoneServiceProjectIDs).ToList();
                if (productsAvailableScreenTypeList.Any())
                {
                    for (int i = 0; i < productsAvailableScreenTypeList.Count; i++)
                    {
                        if (!result)
                            return false;
                        var productsAvailableScreenType = productsAvailableScreenTypeList[i];
                        var entity = new InsuranceProductAvailableScreenSnapshoot
                        {
                            InsuranceOrderServiceProjectId = productsAvailableScreenType.CellPhoneServiceProjectID,
                            ServiceProjectType = (byte)serviceProjectType,
                            AvailableScreenType = productsAvailableScreenType.ScreenTypeCode,
                        };
                        result = AddEntities(entity).ID > 0;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 获取可用屏幕
        /// </summary>
        /// <param name="insuranceOrderServiceProjectId"></param>
        /// <param name="serviceProjectType"></param>
        /// <returns></returns>
        public IQueryable<InsuranceProductAvailableScreenSnapshoot> GetInsuranceProductAvailableScreenSnapshoot(long insuranceOrderServiceProjectId, ServiceProductType serviceProjectType)
        {
            var bServiceProjectType = (byte)serviceProjectType;
            return
                LoadEntities(
                    o => o.InsuranceOrderServiceProjectId == insuranceOrderServiceProjectId && o.ServiceProjectType == bServiceProjectType).AsNoTracking();
        }
    }
}